package cording.example.addressList.controller;

import cording.example.addressList.Service.ContactService;
import cording.example.addressList.mybatis.entity.Contact;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequestMapping("/contacts")
public class ContactController {

    @Autowired
    private ContactService contactService;
    @GetMapping("/")
    ModelAndView getContactListView() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("contact_list");
        return mv;
    }
    // 获取所有联系人数据（前端通过该接口获取联系人数据）
    @GetMapping("")
    public ResponseEntity<List<Contact>> listContacts() {
        List<Contact> contacts = contactService.getAllContacts();
        return ResponseEntity.ok(contacts);
    }
    @GetMapping("/add")
    ModelAndView getAddListView() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("add_contact");
        return mv;
    }

    @GetMapping("/edit")
    ModelAndView getEditListView() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("edit_contact");
        return mv;
    }

    // 搜索联系人
    @GetMapping("/search")
    public ResponseEntity<List<Contact>> searchContacts(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "phoneNumber", required = false) String phoneNumber) {
        List<Contact> contacts;
        if (name != null && !name.isEmpty()) {
            contacts = contactService.findByName(name);
        } else if (phoneNumber != null && !phoneNumber.isEmpty()) {
            contacts = contactService.findByPhoneNumber(phoneNumber);
        } else {
            contacts = contactService.getAllContacts();
        }
        return ResponseEntity.ok(contacts);
    }

    // 获取所有联系人
    /*@GetMapping("/")
    public ResponseEntity<List<Contact>> listContacts() {
        List<Contact> contacts = contactService.findAllContacts();
        return ResponseEntity.ok(contacts);
    }*/

    // 获取联系人详情（编辑联系人时需要数据）
    @GetMapping("/get")
    public ResponseEntity<Contact> getContact(@RequestParam("id") Long id) {
        Contact contact = contactService.findContactById(id);
        return ResponseEntity.ok(contact);
    }

    // 删除联系人
    @DeleteMapping("/delete")
    public ResponseEntity<Void> deleteContact(@RequestParam("id") Long id) {
        contactService.deleteContact(id);
        return ResponseEntity.noContent().build();
    }

    // 添加联系人
    @PostMapping("/add")
    public ResponseEntity<Void> addContact(@RequestBody Contact contact) {
        contactService.addContact(contact);
        return ResponseEntity.ok().build();
    }

    // 更新联系人
    @PostMapping("/save")
    public ResponseEntity<Void> saveContact(@RequestBody Contact contact) {
        contactService.saveContact(contact);
        return ResponseEntity.ok().build();
    }
}



/*
package cording.example.addressList.controller;

import cording.example.addressList.Service.ContactService;
import cording.example.addressList.mybatis.entity.Contact;
import cording.example.addressList.mybatis.mapper.ContactListMapper;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/contacts")
public class ContactController {
    @Autowired
    private ContactService contactService;
    @GetMapping("/search")
    public String searchContacts(@RequestParam(value = "name", required = false) String name,
                                 @RequestParam(value = "phoneNumber", required = false) String phoneNumber,
                                 Model model) {
        List<Contact> contacts;
        if (name != null && !name.isEmpty()) {
            contacts = contactService.findByName(name);
        } else if (phoneNumber != null && !phoneNumber.isEmpty()) {
            contacts = contactService.findByPhoneNumber(phoneNumber);
        } else {
            contacts = contactService.getAllContacts();
        }
        model.addAttribute("contacts", contacts);
        return "contact_list";  // 视图名称
    }
    // 显示联系人列表
    @GetMapping
    public String listContacts(Model model) {
        List<Contact> contacts = contactService.findAllContacts();
        model.addAttribute("contacts", contacts);
        return "contact_list"; // 返回联系人列表页面，Thymeleaf 模板
    }

    // 显示编辑联系人页面
    @GetMapping("/edit")
    public String editContact(@RequestParam("id") Long id, Model model) {
        Contact contact = contactService.findContactById(id);
        model.addAttribute("contact", contact);
        return "edit_contact"; // 编辑联系人页面
    }

    // 删除联系人
    @GetMapping("/delete")
    public String deleteContact(@RequestParam("id") Long id) {
        contactService.deleteContact(id);
        return "redirect:/contacts"; // 删除后重定向到联系人列表
    }

    // 显示添加联系人页面（GET请求）
    @GetMapping("/add")
    public String addContactForm(Model model) {
        // 创建一个空的 Contact 对象并将其传递到视图
        Contact contact = new Contact();
        model.addAttribute("contact", contact);  // contact对象绑定到模型
        return "add_contact"; // 返回Thymeleaf页面
    }

    // 处理添加联系人表单提交（POST请求）
    @PostMapping("/add")
    public String addContact(@ModelAttribute Contact contact) {
        // 调用 Service 层的 addContact 方法将数据保存到数据库
        contactService.addContact(contact);
        return "redirect:/contacts"; // 保存成功后重定向到联系人列表页面
    }

    // 保存新联系人
    @PostMapping("/save")
    public String saveContact(@ModelAttribute Contact contact) {
        contactService.saveContact(contact);
        return "redirect:/contacts"; // 保存后重定向到联系人列表
    }
}

*/
